// pages/index/near.js
Page({

  data: {  store:[],statu:'',store1:''

  },

 
  onLoad: function (options) {
    console.log(options);
    
    if(options.page){
       this.setData({
         page:options.page
       })
    }
     this.setData({
       statu:options.statu
     })
     if(options.statu=='index'){
      this.getnearshop()
     }
     else{ this.getnearshop1()}
   

  },
   //跳转到店铺详情
    navstore_xq(e){
      if(this.data.page!='indexheader')
      {  console.log(e);
     let near_id= e.currentTarget.dataset.id
      let mi=e.currentTarget.dataset.long?e.currentTarget.dataset.long:''
      wx.navigateTo({
        url: '/pages/index/near/store_xq/store_xq?near_id='+near_id+'&statu='+this.data.statu+"&mi="+mi,
      })
      return
    }
    let mi=e.currentTarget.dataset.long?e.currentTarget.dataset.long:''
    let near_id= e.currentTarget.dataset.id
     this.getgoodslist(near_id,mi)

    },
    //获取推荐店铺列表
    getnearshop(){
       wx.request({
         url: 'https://zujieba.com/api/index/index.html',
         success:(res)=>{
              console.log(res);
              let store=res.data.data.store
             
              
              this.setData({
               
                store : store
                
              })
              
         }
       })
    },
      //获取位置近列表
      getnearshop1(){
        wx.request({
          url: 'https://zujieba.com/api/store/list_store.html',
         data:{distance:1,lng:wx.getStorageSync('lng'),lat:wx.getStorageSync('lat')},
          success:(res)=>{
               console.log(res);
               let store1=res.data.data
               store1.forEach((item)=>{item.long=(item.long/1000).toFixed(1)})
               console.log(store1);
               this.setData({
                
                 store1 : store1
                 
               })
               
          }
        })
     },
    makephone(e){
         wx.makePhoneCall({
           phoneNumber: e.currentTarget.dataset.phone,
           success:(res)=>{
             console.log(res);
             
           },
           fail:(res)=>{
             console.log(res);
             
           }
         })
    },
    gohere(e){
      console.log(this.data.store1);
      
      let lat=e.currentTarget.dataset.lat
      let lng=e.currentTarget.dataset.lng
      console.log(lat,lng);
      
      
      
      wx.openLocation({
        name:e.currentTarget.dataset.title,
        latitude: Number(lat),
        longitude:Number(lng),
      })
    },
    //获取设备列表
getgoodslist(near_id,mi){
  wx.request({
    url: 'https://zujieba.com/Api/store/goods.html',
    data:{pid:near_id},
    method:'POST',
    success:(res)=>{
      const pages = getCurrentPages(); //获取页面栈堆
      const prev = pages[pages.length - 2];
      console.log(res);
      
      let goods_id=res.data.data[0].id
      let store1=prev.data.store1
      store1.id=near_id
      store1.title=res.data.data[0].title
      store1.long=mi
      prev.setData({
      goods_id,
      store1
      
     })
     wx.navigateBack({
       complete: (res) => {},
     })
   
    }
  })
},
})